package com.ce.pms.system.custservice.web;

import java.util.ArrayList;
import java.util.List;

import com.ce.pms.base.vo.SearchVO;
import com.ce.pms.base.web.PmsBaseAction;
import com.ce.pms.common.model.SysrefcodeDO;
import com.ce.pms.exception.BusinessException;
import com.ce.pms.system.custservice.service.IReturnVisitItemService;
import com.ce.pms.system.custservice.vo.ReturnVisitItemVO;
import com.ce.util.BlankUtil;
import com.sitechasia.webx.core.annotation.Read;
import com.sitechasia.webx.core.support.Page;

/**
 * 回访项目
 * 
 * @author <a href="mailto:i0o@live.cn">黄腾飞</a>
 * @Created 2008-12-19
 */
public class SystemCustEventReturnVisitItemAction extends PmsBaseAction
		implements ISystemEventAction {
	private static final String		PAGE_EDIT	= "systemCustEventReturnVisitItemEditPage";

	private static final String		PAGE_INIT	= "systemCustEventReturnVisitItemListPage";

	private IReturnVisitItemService	returnVisitItemService;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ce.pms.system.custservice.web.ISystemEventAction#addPage()
	 */
	public String addPage() {
		// TODO Auto-generated method stub
		return PAGE_EDIT;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.ce.pms.system.custservice.web.ISystemEventAction#delete(java.lang
	 * .Long)
	 */
	public void delete(@Read(key = "keyword") Long keyword) {
		try {
			if (this.returnVisitItemService.deleteReturnVisitItemById(keyword)) {
				this.renderText("true");
			} else {
				this.renderText("false");
			}
		} catch (RuntimeException re) {
			re.printStackTrace();
			throw new BusinessException(
					"custservice:system_delete_inused_exception",
					BusinessException.WARNING);
		}
	}

	/**
	 * 编辑
	 * 
	 * @param returnVisitItemVO
	 * @author <a href="mailto:i0o@live.cn">黄腾飞</a>
	 * @created 2008-12-22
	 */
	public void edit(@Read ReturnVisitItemVO returnVisitItemVO) {
		returnVisitItemVO.setValidity("IS_USE".equals(returnVisitItemVO
				.getValidity()) ? "1" : "0");

		// 保留
		returnVisitItemVO.setClazz("001");

		if (BlankUtil.isBlank(returnVisitItemVO.getKeyword())) {
			returnVisitItemVO.setCreator(this.getUserId());
			this.returnVisitItemService.addReturnVisitItem(returnVisitItemVO);
		} else {
			returnVisitItemVO.setModifier(this.getUserId());
			this.returnVisitItemService.editReturnVisitItem(returnVisitItemVO);
		}

		this.renderText("true");
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ce.pms.system.custservice.web.ISystemEventAction#editPage()
	 */
	public String editPage(@Read(key = "keyword") Long keyword) {

		ReturnVisitItemVO returnVisitItemVO = this.returnVisitItemService
				.findObjectVOById(keyword);

		this.setAttribute("returnVisitItemVO", returnVisitItemVO);

		return PAGE_EDIT;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.ce.pms.system.custservice.web.ISystemEventAction#existsName(java.
	 * lang.String)
	 */
	public void existsName(@Read(key = "name") String name) {
		try {
			this.returnVisitItemService.checkExistenceByName(name);
			this.renderText("true");
		} catch (RuntimeException re) {
			this.renderText(this.getLocaleMessage(re.getMessage()));
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ce.pms.system.custservice.web.ISystemEventAction#initMain()
	 */
	public String initMain() {
		this.queryDataList(15, 1);
		return PAGE_INIT;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ce.pms.system.custservice.web.ISystemEventAction#queryDataList()
	 */
	public String queryDataList(
			@Read(key = "ec_crd", defaultValue = "15") int pageSize,
			@Read(key = "ec_p", defaultValue = "1") int pageNo) {
		SearchVO searchVO = new SearchVO();
		searchVO.setPageNo(pageNo);
		searchVO.setPageSize(pageSize);

		Page page = this.returnVisitItemService
				.queryReturnVisitItemListByPage(searchVO);

		List<ReturnVisitItemVO> voList = page.getResult();

		List<ReturnVisitItemVO> returnVisitItemVOList = new ArrayList<ReturnVisitItemVO>();

		// 处理显示
		for (ReturnVisitItemVO voTmp : voList) {
			if ("1".equals(voTmp.getValidity())) {
				voTmp.setValidity(SysrefcodeDO.IS_USE);
			} else {
				voTmp.setValidity(SysrefcodeDO.IS_NOT_USE);
			}

			returnVisitItemVOList.add(voTmp);
		}

		this.setAttribute("queryDataList", returnVisitItemVOList);

		this.setAttribute("totalRows", Integer.valueOf(String.valueOf(page
				.getTotalCount())));

		return PAGE_INIT;
	}

	/**
	 * @param returnVisitItemService the returnVisitItemService to set
	 */
	public void setReturnVisitItemService(
			IReturnVisitItemService returnVisitItemService) {
		this.returnVisitItemService = returnVisitItemService;
	}
}
